<?php
	/*
 	* @version $Id: unblock.php,v 1.2 2010/09/27 03:41:19 marco_manti
 	* @package http://www.gisa.by
 	* @copyright (C) 2006 - 2010 marco_manti(R)
 	* @license http://www.gnu.org/copyleft/gpl.html GNU/GPL
 	* @description Script delete blockers from database
 	*
 	*/

        require_once(dirname(dirname(__FILE__)).'/includes/library.php');

        // init global classes
        $dbo = new DB();
        $helper = new HELPER();
        $config = new CONFIG();
        
        $endline = chr(13).chr(10);
        $counter = 0;
        $today = date('Y-m-d H:i');
        $title = ' BLOCK REPORT: ';
        $email_title = 'GISA.by ::' . $title;
        $email = $config->server_root;
        $logmsg = $mysqlerrlog = '';
        $tables = array();

        // mail headers
        $headers  = "MIME-Version: 1.0\r\n";
        $headers .= "Content-type: text/html; charset=windows-1251\r\n";
        $headers .= "From: GISA.by <gisaby@gmail.com>\r\n";
        $headers .= "X-Mailer: PHP/" . phpversion();

        // header
        $body .= '<div><strong>Starting check for blocked fields on GISA.BY</strong></div>';
        $body .= '<div>Local server time: '.$today.'</div>';
        $body .= '<div style="height:2px; border:1px solid #CCC;"></div>';

        // connect to DB
        if (!$dbo->connect()) {
            $logmsg .= $today.$title.'ERROR - connect to database ['.$dbo->error().'];'.$endline;
            $mysqlerrlog .= $today.$title.' '.$dbo->error().$endline;
            mail($email, $email_title, 'ERROR - connect to database ['.$dbo->error().'];', $headers);
        }

        // get tables
        $tables = $dbo->getArray("SHOW TABLES");
        if (!$tables) {
            $logmsg .= $today.$title.'ERROR - extract tables ['.$dbo->error().'];'.$endline;
            $mysqlerrlog .= $today.$title.' '.$dbo->error().$endline;
            mail($email, $email_title, 'ERROR - extract tables ['.$dbo->error().'];', $headers);
        }
        
        foreach ($tables as $tn) {

           $fields = $dbo->getObjectsArray("SHOW FIELDS FROM ".$tn);
           foreach ($fields as $field) {
               $result[$field->Field] = preg_replace("/[(0-9)]/",'', $field->Type );
           }

           $foundCO = false;
           $foundCOT = false;
           $foundE = false;

           $foundCO = isset($result['checked_out']);
           $foundCOT = isset($result['checked_out_time']);
           $foundE = isset($result['editor']);

           if ($foundCO && $foundCOT) {
               if ($foundE) {
                    $query = "UPDATE $tn"
                           . " SET checked_out = 0, checked_out_time = '0000-00-00 00:00:00', editor = NULL"
                           . " WHERE checked_out > 0";
               } else {
                    $query = "UPDATE $tn"
                           . " SET checked_out = 0, checked_out_time = '0000-00-00 00:00:00' "
                           . " WHERE checked_out > 0";
               }

               $rows = $dbo->query($query);
               if ($dbo->_lastResult < 0) {
                   $logmsg .= $today.$title.'ERROR - update fields at '.$tn.' ['.$dbo->error().'];'.$endline;
                   $mysqlerrlog .= $today.$title.' '.$dbo->error().$endline;
                   mail($email, $email_title, 'ERROR - update fields at '.$tn.' ['.$dbo->error().'];', $headers);
               }

               if ($rows) $counter += $rows; 
           }
           $result = array();
        }

        // generating output message
        if ($counter > 0)
            $logmsg .= $today.$title.'SUCCESS - '.$counter.' object blocked;'.$endline;
        else
            $logmsg .= $today.$title.'SUCCESS - no object blocked;'.$endline;

        // write state to log
        $helper->writeToLog($logmsg);
        
        // write SQL error if present
        if ($mysqlerrlog != '') {
            $helper->writeToFile($mysqlerrlog, 'a+', 'mysql_errors.log');
        }
?>